<?php

/*------------------------------------------------
*
*   Explay Engine Content Management System.
*   Copyright © 2008 Golovdinov Alexander.
*
*-------------------------------------------------
*
*   Oficial site: wwww.alex-home.spb.ru/explay.html
*   Contact e-mail: golovdinov@gmail.com
*
*   GNU General Public License original source:
*   http://www.gnu.org/licenses/gpl-3.0.html
*
------------------------------------------------*/

if (!defined ('EXPLAY')) exit;

past_path (array ( array ('', 'Запрос к БД') ));

if (isset($_GET['send'])) {
	if ($_POST['code'] == '') site_error ('Не заполнено поле');
	else {		
		$code_ = stripslashes($_POST['code']);
		
		if (substr($code_, (strlen($code_)-1), 1) == ';') $code_ = substr($code_, 0, (strlen($code_)-1));
		
		$code_ = explode (';', $code_);
		
		$mes_t = 0;
		$mes_f = '';
		
		foreach ($code_ as $code) {
			if (isset($_POST['result'])) {
					
					
					if ($query = @mysql_query ($code)) {
					$rows = mysql_num_rows ($query);
					$cols = mysql_num_fields ($query);
					$table = '';
					$table .= "\n\t<tr>";
					for ($w=0; $w<$cols; $w++) {
						$table .= "\n\t\t<td valign=top><center><b>".mysql_field_name($query, $w)."</b></td>";
					}
					$table .= "\n\t</tr>";
				
					for ($i=0; $i<$rows; $i++) {
						$table .= "\n\t<tr>";
						for ($j=0; $j<$cols; $j++) {
							$table .= "\n\t\t<td valign=top>".mysql_result ($query, $i, $j)."&nbsp;</td>";
						}
						$table .= "\n\t</tr>";
					}
					past_table ('Результат запроса', '<table class="main_text" border="1">'.$table.'</table>');
				}
				else site_error ('Запрос не был выполнен!<br><font color="red">'.mysql_error().'</font>');
				
			
			} else {				
				
				if (@mysql_query ($code)) $mes_t++;
				else $mes_f .= "\n<li>Запрос не был выполнен!<br><font color=red>".mysql_error()."</font>";
				
			}
		}
		
		if ($mes_t != 0 && $mes_f == '') site_message ('Запрос выполнен');
		if ($mes_f != '') site_error ($mes_f);
	}
}

$option = '';

$get_tables = @mysql_query ("SHOW TABLES");
while ($tables = @mysql_fetch_array($get_tables)) {
	$title = $tables[0];
	if (substr($title, 0, strlen(DB_PEREFIX)) == DB_PEREFIX) {
		$option .= "\r\n$title";
	}
}

if (isset($_POST['result'])) $cbox = 'checked';

$form = "<form method=\"post\" action=\"admin.php?do=mysql&send\" name=\"record\">
<table class=\"main_text\" cellpadding=\"2\" cellspacing=\"0\" width=\"100%\">
	<tr>
		<td>MySQL - код:</td>
	</tr>
	<tr>
		<td colspan=\"2\" valign=\"top\" align=\"left\">
			<div style=\"float:right;width:25%\">Список таблиц:<br /><textarea readonly rows=\"14\">$option</textarea></div>
			<div style=\"float:left;width:70%\"><textarea name=\"code\" style=\"width:100%\" rows=\"15\">".$_POST['code']."</textarea></div>
		</td>
	</tr>
	<tr>
		<td>
			<span class=\"important_checkbox\"><input type=\"checkbox\" name=\"result\" value=\"true\" id=\"result\" $cbox></span> <label for=\"result\">Вывести результат</label><p>
			<input type=\"button\" value=\"Сбросить\" onclick=\"document.record.code.value='';\">
			<span class=\"important_button\"><input type=\"submit\" value=\"Пошел!\"></span>
		</td>
	</tr>
</table>
</form>";

past_table ('Запрос к базе данных', $form);

come_back ('панели администратора', 'admin.php');
